package fun.stgoder.test1;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class TestCyclicBarrier {
    public static void main(String[] args) throws InterruptedException {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(10, () -> {
            System.out.println("begin");
        });

        for (int i = 0; i < 9; i++) {
            final int j = i;
            new Thread(() -> {
                try {
                    System.out.println(j);
//                    TimeUnit.SECONDS.sleep(5);
                    //cyclicBarrier.await(3, TimeUnit.SECONDS);
                    cyclicBarrier.await();
                    System.out.println("after await " + j);

                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                } /*catch (TimeoutException e) {
                    e.printStackTrace();
                }*/
            }).start();


        }
    }
}
